package com.todod.base.module.base.dal.mysql.site;

import java.util.*;

import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.todod.base.framework.mybatis.core.mapper.BaseMapperX;
import com.todod.base.module.base.dal.dataobject.site.SiteDO;
import lombok.Data;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import com.todod.base.module.base.controller.admin.site.vo.*;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 现场 Mapper
 *
 * @author 超管儿
 */
@Mapper
public interface SiteMapper extends BaseMapperX<SiteDO> {

    default PageResult<SiteDO> selectPage(SitePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SiteDO>()
                .eqIfPresent(SiteDO::getWellId, reqVO.getWellId())
                .likeIfPresent(SiteDO::getWellName, reqVO.getWellName())
                .eqIfPresent(SiteDO::getWellCode, reqVO.getWellCode())
                .eqIfPresent(SiteDO::getWellType, reqVO.getWellType())
                .eqIfPresent(SiteDO::getWellCategory, reqVO.getWellCategory())
                .eqIfPresent(SiteDO::getOilField, reqVO.getOilField())
                .eqIfPresent(SiteDO::getOilFieldBlock, reqVO.getOilFieldBlock())
                .eqIfPresent(SiteDO::getState, reqVO.getState())
                .betweenIfPresent(SiteDO::getDrillingDate, reqVO.getDrillingDate())
                .betweenIfPresent(SiteDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SiteDO::getCreateTime));
    }


    /**
     * 获取现场已经启用的井数量
     */
    @Select("SELECT COUNT(*) FROM base_site where deleted = 0 AND state = 1")
    public int getWellNum();

    /**
     * 验证存在
     **/
    @Select("SELECT * FROM base_site WHERE deleted = 0 AND well_code = #{wellCode}")
     public SiteDO getByWellCode(String wellCode);

    /**
    * 删除
    **/
    @Update("UPDATE  base_site SET deleted = 1 WHERE well_code = #{wellCode}")
    public void  deleteByWellCode(String wellCode);


    /**
     * 启用
     */
    @Update("UPDATE base_site SET state = 1   WHERE well_code = #{wellCode} ")
    public void enable(String wellCode);

    /**
     * 禁用
     */
    @Update("UPDATE base_site SET state = 0   WHERE well_code = #{wellCode} ")
    public void disable(String wellCode);



    /**
     * 获取现场列表信息
     **/
    @Select("<script>" +
            "SELECT s.well_id,s.well_code, s.well_name  " +
            "FROM base_site s " +
            "<where>" +
            "    s.deleted = 0 " +
            "    <if test='params != null and params != \"\"'>" +
            "         AND (s.well_code LIKE '%${params}%') " +
            "    </if>" +
            "    ORDER BY s.create_time" +
            "</where>"
            + "</script>")
    public List<SiteDO> getListSite(String params);



}